package com.code.google.wormon;

import java.awt.AlphaComposite;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import javax.swing.ImageIcon;

import com.code.google.wormon.base.Game;
import com.code.google.wormon.base.GameObject;
import com.code.google.wormon.base.GameScene;

/**
 * The WormOn Game.
 * 
 * @author volnei
 * 
 */
public class WormonGame extends Canvas implements Game {
	private static final long serialVersionUID = 5426492486408035887L;
	private Vector<GameObject> gameObjects;
	private Vector<GameScene> gameScenes;

	@Override
	public void start() {
		this.requestFocus();
		this.setIgnoreRepaint(true);
		this.createBufferStrategy(3);

		WormonSoundTrack.TRACK_1.play(50);

		gameScenes = new Vector<GameScene>();
		gameScenes.add(new WormonScene(this, 1600, 400, new ImageIcon(getClass().getClassLoader().getResource("com/code/google/wormon/resources/b1.png")).getImage(), true, false));
		gameScenes.add(new WormonScene(this, 1600, 400, new ImageIcon(getClass().getClassLoader().getResource("com/code/google/wormon/resources/b2.png")).getImage(), true, false));

		gameObjects = new Vector<GameObject>();
		gameObjects.add(new WormonGuy(this));
		gameObjects.add(new WormonGangster(this));
		Random random = new Random();
		for (int i = 0; i < 100; i++) {
			gameObjects.add(new WormonVitamin(random.nextInt(400), random.nextInt(300)));
		}

		addKeyListener(new KeyAdapter() {

			@Override
			public void keyReleased(KeyEvent e) {
				fireKeyEvent(e);
			}

			@Override
			public void keyPressed(KeyEvent e) {
				fireKeyEvent(e);
			}
		});
	}

	@Override
	public void doGraphics() {

		Graphics2D g2d = (Graphics2D) getBufferStrategy().getDrawGraphics().create();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);

		// print scenes
		for (GameScene gameScene : gameScenes) {
			gameScene.draw(g2d);
		}

		// print objects
		for (GameObject gameObject : gameObjects) {
			gameObject.draw(g2d);
		}

		Graphics2D panel = (Graphics2D) g2d.create(0, 0, getWidth(), 50);
		panel.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .7f));
		panel.setColor(Color.WHITE);
		panel.fillRect(0, 0, getWidth(), 50);
		panel.dispose();

		g2d.dispose();
		if (!getBufferStrategy().contentsLost()) {
			getBufferStrategy().show();
		}

	}

	double d1 = 0.00008d;
	double d2 = 0.00001d;

	@Override
	public void doLogics() {

		// print scenes

		gameScenes.get(1).setHPos(d1 += .00005d);
		gameScenes.get(0).setHPos(d2 += .00001d);

		for (GameObject g1 : gameObjects) {
			g1.doLogic();
		}

		// detect colisions
		for (GameObject g1 : gameObjects) {
			for (GameObject g2 : gameObjects) {
				if (g1 == g2)
					continue;
				if (g1.getBounds().intersects(g2.getBounds())) {
					WormonSoundFx.SHOOT.play();
					g2.collided(g1);
					g1.collided(g2);
				}
			}
		}

		List<GameObject> remove = new ArrayList<GameObject>();
		for (GameObject g1 : gameObjects) {
			if (g1 instanceof WormonVitamin) {
				WormonVitamin vitamin = (WormonVitamin) g1;
				if (vitamin.isMarkForRemove()) {
					remove.add(g1);
				}
			}
		}
		gameObjects.removeAll(remove);
	}

	@Override
	public void stop() {
	}

	private void fireKeyEvent(KeyEvent e) {
		for (GameObject gameObject : gameObjects) {
			gameObject.react(e);
		}
	}

	@Override
	public Integer getScreenHeight() {
		return 300;
	}

	@Override
	public Integer getScreenWidth() {
		return 800;
	}
	
	public GameObject getGuy(){
		for (int i = 0; i<= gameObjects.capacity(); i++){
			if (gameObjects.get(i).isGuy) {
				return gameObjects.get(i);
			}
		}
		return null;
	}

}